<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>proto2.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_testing_proto2_proto2.js.html">proto2.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2012 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Test helpers to compare goog.proto2.Messages.
<a name="line17"></a> *
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>goog.provide(&#39;goog.testing.proto2&#39;);
<a name="line21"></a>
<a name="line22"></a>goog.require(&#39;goog.proto2.Message&#39;);
<a name="line23"></a>goog.require(&#39;goog.testing.asserts&#39;);
<a name="line24"></a>
<a name="line25"></a>
<a name="line26"></a>/**
<a name="line27"></a> * Compares two goog.proto2.Message instances of the same type.
<a name="line28"></a> * @param {!goog.proto2.Message} expected First message.
<a name="line29"></a> * @param {!goog.proto2.Message} actual Second message.
<a name="line30"></a> * @param {string} path Path to the messages.
<a name="line31"></a> * @return {string} A string describing where they differ. Empty string if they
<a name="line32"></a> *     are equal.
<a name="line33"></a> * @private
<a name="line34"></a> */
<a name="line35"></a>goog.testing.proto2.findDifferences_ = function(expected, actual, path) {
<a name="line36"></a>  var fields = expected.getDescriptor().getFields();
<a name="line37"></a>  for (var i = 0; i &lt; fields.length; i++) {
<a name="line38"></a>    var field = fields[i];
<a name="line39"></a>    var newPath = (path ? path + &#39;/&#39; : &#39;&#39;) + field.getName();
<a name="line40"></a>
<a name="line41"></a>    if (expected.has(field) &amp;&amp; !actual.has(field)) {
<a name="line42"></a>      return newPath + &#39; should be present&#39;;
<a name="line43"></a>    }
<a name="line44"></a>    if (!expected.has(field) &amp;&amp; actual.has(field)) {
<a name="line45"></a>      return newPath + &#39; should not be present&#39;;
<a name="line46"></a>    }
<a name="line47"></a>
<a name="line48"></a>    if (expected.has(field)) {
<a name="line49"></a>      var isComposite = field.isCompositeType();
<a name="line50"></a>
<a name="line51"></a>      if (field.isRepeated()) {
<a name="line52"></a>        var expectedCount = expected.countOf(field);
<a name="line53"></a>        var actualCount = actual.countOf(field);
<a name="line54"></a>        if (expectedCount != actualCount) {
<a name="line55"></a>          return newPath + &#39; should have &#39; + expectedCount + &#39; items, &#39; +
<a name="line56"></a>              &#39;but has &#39; + actualCount;
<a name="line57"></a>        }
<a name="line58"></a>
<a name="line59"></a>        for (var j = 0; j &lt; expectedCount; j++) {
<a name="line60"></a>          var expectedItem = expected.get(field, j);
<a name="line61"></a>          var actualItem = actual.get(field, j);
<a name="line62"></a>          if (isComposite) {
<a name="line63"></a>            var itemDiff = goog.testing.proto2.findDifferences_(
<a name="line64"></a>                /** @type {!goog.proto2.Message} */ (expectedItem),
<a name="line65"></a>                /** @type {!goog.proto2.Message} */ (actualItem),
<a name="line66"></a>                newPath + &#39;[&#39; + j + &#39;]&#39;);
<a name="line67"></a>            if (itemDiff) {
<a name="line68"></a>              return itemDiff;
<a name="line69"></a>            }
<a name="line70"></a>          } else {
<a name="line71"></a>            if (expectedItem != actualItem) {
<a name="line72"></a>              return newPath + &#39;[&#39; + j + &#39;] should be &#39; + expectedItem +
<a name="line73"></a>                  &#39;, but was &#39; + actualItem;
<a name="line74"></a>            }
<a name="line75"></a>          }
<a name="line76"></a>        }
<a name="line77"></a>      } else {
<a name="line78"></a>        var expectedValue = expected.get(field);
<a name="line79"></a>        var actualValue = actual.get(field);
<a name="line80"></a>        if (isComposite) {
<a name="line81"></a>          var diff = goog.testing.proto2.findDifferences_(
<a name="line82"></a>              /** @type {!goog.proto2.Message} */ (expectedValue),
<a name="line83"></a>              /** @type {!goog.proto2.Message} */ (actualValue),
<a name="line84"></a>              newPath);
<a name="line85"></a>          if (diff) {
<a name="line86"></a>            return diff;
<a name="line87"></a>          }
<a name="line88"></a>        } else {
<a name="line89"></a>          if (expectedValue != actualValue) {
<a name="line90"></a>            return newPath + &#39; should be &#39; + expectedValue + &#39;, but was &#39; +
<a name="line91"></a>                actualValue;
<a name="line92"></a>          }
<a name="line93"></a>        }
<a name="line94"></a>      }
<a name="line95"></a>    }
<a name="line96"></a>  }
<a name="line97"></a>
<a name="line98"></a>  return &#39;&#39;;
<a name="line99"></a>};
<a name="line100"></a>
<a name="line101"></a>
<a name="line102"></a>/**
<a name="line103"></a> * Compares two goog.proto2.Message objects. Gives more readable output than
<a name="line104"></a> * assertObjectEquals on mismatch.
<a name="line105"></a> * @param {!goog.proto2.Message} expected Expected proto2 message.
<a name="line106"></a> * @param {!goog.proto2.Message} actual Actual proto2 message.
<a name="line107"></a> * @param {string=} opt_failureMessage Failure message when the values don&#39;t
<a name="line108"></a> *     match.
<a name="line109"></a> */
<a name="line110"></a>goog.testing.proto2.assertEquals = function(expected, actual,
<a name="line111"></a>    opt_failureMessage) {
<a name="line112"></a>  var failureSummary = opt_failureMessage || &#39;&#39;;
<a name="line113"></a>  if (!(expected instanceof goog.proto2.Message) ||
<a name="line114"></a>      !(actual instanceof goog.proto2.Message)) {
<a name="line115"></a>    goog.testing.asserts.raiseException(failureSummary,
<a name="line116"></a>        &#39;Bad arguments were passed to goog.testing.proto2.assertEquals&#39;);
<a name="line117"></a>  }
<a name="line118"></a>  if (expected.constructor != actual.constructor) {
<a name="line119"></a>    goog.testing.asserts.raiseException(failureSummary,
<a name="line120"></a>        &#39;Message type mismatch: &#39; + expected.getDescriptor().getFullName() +
<a name="line121"></a>        &#39; != &#39; + actual.getDescriptor().getFullName());
<a name="line122"></a>  }
<a name="line123"></a>  var diff = goog.testing.proto2.findDifferences_(expected, actual, &#39;&#39;);
<a name="line124"></a>  if (diff) {
<a name="line125"></a>    goog.testing.asserts.raiseException(failureSummary, diff);
<a name="line126"></a>  }
<a name="line127"></a>};
</pre>


</body>
</html>
